package com.cskaoyan.controller.wx;

import com.cskaoyan.model.admin.bo.ListBo;
import com.cskaoyan.model.admin.vo.AdBaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.model.admin.vo.WxOrderGoodsVo;
import com.cskaoyan.model.vx.bo.WxOrderCommentBo;
import com.cskaoyan.model.vx.vo.WxOrderDetailVo;
import com.cskaoyan.service.WxOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @description: TODO
 * @author: jiaolei
 * @date: 2021/12/2 11:16
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;
    @GetMapping("list")
    public BaseRespVo list(ListBo listBo,Short showType,HttpSession session){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        AdBaseData adBaseData=wxOrderService.list(listBo,showType,userId);
        return BaseRespVo.ok(adBaseData);
    }
    @GetMapping("detail")
    public BaseRespVo detail(Integer orderId){
        WxOrderDetailVo wxOrderDetailVo=wxOrderService.detail(orderId);
        return BaseRespVo.ok(wxOrderDetailVo);
    }
    @PostMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map,HttpSession session){
        Integer userId = (Integer) session.getAttribute("wxUserId");
        Integer orderId = (Integer) map.get("orderId");
        int rows=wxOrderService.cancel(orderId,userId);
        return BaseRespVo.ok(null);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        int rows=wxOrderService.delete(orderId);
        return BaseRespVo.ok(null);
    }
    @PostMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBo wxOrderCommentBo){
        int rows=wxOrderService.comment(wxOrderCommentBo);
        return BaseRespVo.ok(null);
    }
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        Integer rows=wxOrderService.refund(orderId);
        return BaseRespVo.ok(null);
    }
    @PostMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        Integer rows=wxOrderService.confirm(orderId);
        return BaseRespVo.ok(null);
    }
    @GetMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){
        WxOrderGoodsVo wxOrderGoodsVo=wxOrderService.goods(orderId,goodsId);
        return BaseRespVo.ok(wxOrderGoodsVo);
    }

    @PostMapping("submit")
    public BaseRespVo submit(@RequestBody Map<String, String> map, HttpSession session){
        Integer addressId = null;
        Integer cartId = null;
        Integer couponId = null;
        String message = "";
        Integer userCouponId = null;
        Integer userId = null;
        userId = (Integer) session.getAttribute("wxUserId");
        if(userId == null){
            return BaseRespVo.fail(501,"用户未登录");
        }
        try {
            addressId = Integer.parseInt(map.get("addressId"));
            cartId = Integer.parseInt(map.get("cartId"));
            couponId = Integer.parseInt(map.get("couponId"));
            userCouponId = Integer.parseInt(map.get("userCouponId"));
            message = map.get("cartId");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return BaseRespVo.fail(404,"参数错误");
        }

        Map mapResp = wxOrderService.wxOrderSubmit(addressId, cartId, couponId, message, userCouponId, userId);
        return BaseRespVo.ok(mapResp);
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map){
        String orderId1 = map.get("orderId").toString();
        Integer orderId = Integer.parseInt(orderId1) ;
        wxOrderService.updateStatus(orderId);
        return BaseRespVo.ok(null);
    }
}
